export default {
   data () {
    return {
      // form
      form: this.$form.createForm(this),

      // state
      state: {
        time: 60,
        loginBtn: false,
        // login type: 0 email, 1 username, 2 telephone
        loginType: 0,
        smsSendBtn: false,
      },
    }
  },

  methods: {
    // send code email
    sendMobileCode () {
      console.log(121212)
      const { mobile } = this
      const data = { mobile }
      this.sendCode([], data)
    },

    handleSubmit () {
      const { state } = this

      state.loginBtn = true

      return new Promise((resolve, reject) => {
        this.validateFieldsForm()
          .then(() => {
            this.submitForm()
              .then((data) => {
                resolve(data)
              })
              .catch((error) => {
                reject(error)
              })
              .finally(() => {
                state.loginBtn = false
              })
          })
          .catch((error) => {
            reject(error)
            state.loginBtn = false
          })
      })
    },

    submitCallBack () {
      this.$router.push({ path: '/index/dashboard' })
    },

    resetFields () {
      this.resetCodeStatus()

      this.form.resetFields()
    },

    // form validateFields
    validateFieldsForm () {
      return new Promise((resolve, reject) => {
        const {
          form: { validateFields },
        } = this

        const validateFieldsKey = ['code']

        validateFields(validateFieldsKey, { force: true, first: true }, (err, values) => {
          if (!err) {
            resolve()
            return false
          }
          reject(false)
        })
      })
    },

    // Retrieve password
    submitForm () {
      const validateFieldsKey = ['code']

      return new Promise((resolve, reject) => {
        const fields = this.form.getFieldsValue(validateFieldsKey)
        const { mobile, email } = this

        const data = {
          ...fields,
          mobile,
          email,
        }

        this.$store.dispatch('LoginMobileVerifyCode', data)
          .then(data => resolve(data))
          .catch(error => reject(error))
      })
    },
  },
}
